package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditorChoosePhotoActivity extends BaseActivity implements OnItemClickListener,View.OnClickListener, TabLayout.BaseOnTabSelectedListener {
    public static final String KeyType = "KeyType";
    EditorChoosePhotoAdapter editorChoosePhotoAdapter;
    List<MoreBean.DataBean> dataBeanList = new ArrayList<>();
    TabLayout tabLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_choose_photo);
        final RecyclerView recyclerView = findViewById(R.id.recyclerview);
        tabLayout = findViewById(R.id.tablayout);
        tabLayout.addOnTabSelectedListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        editorChoosePhotoAdapter = new EditorChoosePhotoAdapter(dataBeanList);
        editorChoosePhotoAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(editorChoosePhotoAdapter);
        enableBack();
        setSubTitle("发帖",this);
        switch (getIntent().getIntExtra(KeyType, 1)) {
            case 1:
                setTitle("活动");
                break;
            case 2:
                setTitle("推荐");
                break;
            case 3:
                setTitle("咨询");
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        dataBeanList.clear();
        editorChoosePhotoAdapter.notifyDataSetChanged();
        webApi.getEditorChoosePhoto(getIntent().getIntExtra(KeyType, 1),tabLayout.getSelectedTabPosition()+1).enqueue(new Callback<MoreBean>() {
            @Override
            public void onResponse(Call<MoreBean> call, Response<MoreBean> response) {
                if (response.body().isResult()&&!isFinishing()) {
                    dataBeanList.clear();
                    dataBeanList.addAll(response.body().getData());
                    for(MoreBean.DataBean dataBean:dataBeanList){
                        dataBean.setType("1");
                    }
                    editorChoosePhotoAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<MoreBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int viewID,int position,int state) {
        Intent intent = new Intent(this, EditorChoosePhotoDetailActivity.class);
        intent.putExtra(EditorChoosePhotoDetailActivity.KeyID, dataBeanList.get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.text_sub_title:
                startActivity(new Intent(this,SendCommentActivty.class));
                break;
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        onStart();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
